import 'package:assistant_zhao/booking_model.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class BookingPage extends StatefulWidget {
  @override
  State<BookingPage> createState() => _BookingPageState();
}

class _BookingPageState extends State<BookingPage> {

  List<BookingModel> bookings = [
    BookingModel(bookingId: '00000001', departurePoint: 'Hong Kong', destinationPoint: 'Singapore', status: '进行中', orderTime: DateTime.now()),
    BookingModel(bookingId: '00000002', departurePoint: 'Hong Kong', destinationPoint: 'Singapore', status: '进行中', orderTime: DateTime.now()),
    BookingModel(bookingId: '00000003', departurePoint: 'Hong Kong', destinationPoint: 'Singapore', status: '进行中', orderTime: DateTime.now()),
    BookingModel(bookingId: '00000004', departurePoint: 'Hong Kong', destinationPoint: 'Singapore', status: '已取消', orderTime: DateTime.now()),
    BookingModel(bookingId: '00000005', departurePoint: 'Hong Kong', destinationPoint: 'Singapore', status: '进行中', orderTime: DateTime.now()),
    BookingModel(bookingId: '00000006', departurePoint: 'Hong Kong', destinationPoint: 'Singapore', status: '进行中', orderTime: DateTime.now()),
    BookingModel(bookingId: '00000007', departurePoint: 'Hong Kong', destinationPoint: 'Singapore', status: '已取消', orderTime: DateTime.now()),
    BookingModel(bookingId: '00000008', departurePoint: 'Hong Kong', destinationPoint: 'Singapore', status: '已取消', orderTime: DateTime.now()),
    BookingModel(bookingId: '00000009', departurePoint: 'Hong Kong', destinationPoint: 'Singapore', status: '已取消', orderTime: DateTime.now()),
    BookingModel(bookingId: '00000010', departurePoint: 'Hong Kong', destinationPoint: 'Singapore', status: '已完成', orderTime: DateTime.now()),
    BookingModel(bookingId: '00000011', departurePoint: 'Hong Kong', destinationPoint: 'Singapore', status: '已完成', orderTime: DateTime.now()),
    BookingModel(bookingId: '00000012', departurePoint: 'Hong Kong', destinationPoint: 'Singapore', status: '已完成', orderTime: DateTime.now()),
    BookingModel(bookingId: '00000013', departurePoint: 'Hong Kong', destinationPoint: 'Singapore', status: '已完成', orderTime: DateTime.now()),
    BookingModel(bookingId: '00000014', departurePoint: 'Hong Kong', destinationPoint: 'Singapore', status: '已完成', orderTime: DateTime.now()),
    BookingModel(bookingId: '00000015', departurePoint: 'Hong Kong', destinationPoint: 'Singapore', status: '已完成', orderTime: DateTime.now()),
    BookingModel(bookingId: '00000016', departurePoint: 'Hong Kong', destinationPoint: 'Singapore', status: '已完成', orderTime: DateTime.now()),
  ];


  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Container(
            margin: EdgeInsets.symmetric(vertical: 12, horizontal: 16),
            child: Text(
              '全部订单',
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.w500,
                color: Colors.black,
              ),
              textAlign: TextAlign.left,
            ),
          ),
          Expanded(
            child: Container(
              margin: EdgeInsets.symmetric(horizontal: 8),
              child: ListView.builder(
              itemCount: bookings.length,
              itemBuilder: (context, index) {
                final order = bookings[index];
                return Dismissible(
                  key: Key(order.bookingId),
                  background: Container(
                    color: Colors.red, // 删除项的背景颜色
                    padding: EdgeInsets.symmetric(horizontal: 20), // 水平内边距
                    alignment: AlignmentDirectional.centerStart, // 对齐到开始方向（左侧）
                    child: Icon(Icons.delete, color: Colors.white), // 左侧的图标
                  ),
                  secondaryBackground: Container(
                    color: Colors.red, // 删除项的背景颜色
                    padding: EdgeInsets.symmetric(horizontal: 20), // 水平内边距
                    alignment: AlignmentDirectional.centerEnd, // 对齐到结束方向（右侧）
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.end, // 将内容放在行的末尾
                      children: [
                        Text(
                          '删除',
                          style: TextStyle(
                            color: Colors.white,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        Icon(Icons.delete, color: Colors.white), // 右侧的图标
                      ],
                    ),
                  ),
                  onDismissed: (direction) {
                    setState(() {
                      bookings.removeAt(index);
                    });
                  },
                  child: Card(
                    margin: EdgeInsets.all(8),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8),
                      side: BorderSide(color: Colors.grey.shade300, width: 1),
                    ),
                    child: ListTile(
                      title: Text('订舱号: ' + order.bookingId),
                      subtitle: Container(
                        margin: EdgeInsets.only(top: 8),
                        child: Text('航线: ' + '从 ${order.departurePoint} 到 ${order.destinationPoint}')
                      ),
                      trailing: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          Text(order.status,
                              style: TextStyle(color: getColorForStatus(order.status))),
                          Text(DateFormat('yyyy-MM-dd – kk:mm').format(order.orderTime)),
                        ],
                      ),
                    ),
                  ),
                );
              },
                        ),
            )
          )
        ],
      ),
    );
  }

  Color getColorForStatus(String status) {
  switch(status) {
    case '已完成':
      return Colors.green;
    case '进行中':
      return Colors.orange;
    case '已取消':
      return Colors.grey;
    default:
      return Colors.black;
  }
}
}